/* 
 * File:   mainDriver.cpp
 * Author: AbDuLlaH
 * 
 * Created on February 4, 2015, 2:52 PM
 */

#include "mainDriver.h"

mainDriver::mainDriver() 
{

}

MemberAccount mainDriver::login()
{
    fstream infile;
    
    infile.open("authorization.txt", ios::in);
    
    if(!infile)
    {
        cout << "Error: No authorization.txt" << endl;
        exit(-1);
    }
    
    string temp,tempCusID,tempCusName,tempCusPassword,tempType,tempAuthenticate,others;
    int tempRwdPt;
    string delimiter = "|";
    size_t pos = 0;
    
    while(getline(infile,temp))
    {
         pos = temp.find(delimiter);
         tempCusID = temp.substr(0,pos);
         temp.erase(0,pos + delimiter.length());
        
         pos = temp.find(delimiter);
         tempCusName = temp.substr(0,pos);
         temp.erase(0,pos + delimiter.length());

         pos = temp.find(delimiter);
         tempCusPassword = temp.substr(0,pos);
         temp.erase(0,pos + delimiter.length());

         pos = temp.find(delimiter);
         others = temp.substr(0,pos);
         temp.erase(0,pos + delimiter.length());

         pos = temp.find(delimiter);
         tempType = temp.substr(0,pos);
         temp.erase(0,pos + delimiter.length());

         tempAuthenticate = temp;
         
         tempRwdPt = atoi(others.c_str());
        //tempCusID = decryptLoginDetails(tempCusID);
        
        //getline(infile,tempCusName,'|');
       // tempCusName = decryptLoginDetails(tempCusName);
        
        //getline(infile,tempCusPassword,'|');
        //tempCusPassword = decryptLoginDetails(tempCusPassword);
        
        //getline(infile,others,'|');
        
        //stringstream ss;
        //ss << others;
        
        //tempRwdPt = atoi((ss.str()).c_str());
        
        //getline(infile,type,'|');
        //type = decryptLoginDetails(type);
        
        //getline(infile,authenticate,'|');
        //authenticate = decryptLoginDetails(authenticate);
        
        if(tempType == "member")
        {
            MemberAccount* tempAcc = new MemberAccount;
            
            tempAcc->setMemberID(tempCusID);
            tempAcc->setMemberName(tempCusName);
            tempAcc->setMemberPassword(tempCusPassword);
            tempAcc->setRewardPoints(tempRwdPt);
            tempAcc->setType("member");
            
            if(tempAuthenticate == "notOk")
                tempAcc->setAuthenticate(false);
            else if (tempAuthenticate == "ok")
                tempAcc->setAuthenticate(true);
            
            allLogVec.push_back(tempAcc);
        }
        else if (tempType == "admin")
        {
            cout << tempCusName << "is admin" << endl;
        }
    }
    
    infile.close();
    
    vector<MemberAccount*>::iterator p;
    string uname,pwd;
    bool userOK = false,passOK = false;
    int count = 0;
    
    while(!userOK)
    {
        p = allLogVec.begin();
        count = 0;
        
        cout << "Enter Member ID : ";
        cin >> uname;
        
        if(isalpha(uname[0]))
            uname[0] = toupper(uname[0]);
        
        while(p != allLogVec.end() && !userOK)
        {  
            if(uname == (*p)->getMemberID())
            {
                userOK = true;
            }
            else
            {
               ++count;
               ++p;
            }
        }
        
        if(!userOK)
        {
            cout << "Error: Member ID not found. Re-";
        }
    }
    
    if(userOK)
    {
        cout << "Enter Password : ";
        cin >> pwd;
   
        while(!passOK)
        {
            if(pwd == (*(allLogVec[count])).getMemberPassword())
            {
                passOK = true;
            }
            
            if(!passOK)
            {
                cout << "Error: Incorrect Password. Re-enter Password: ";
                cin >> pwd;
            }
        }
    }
    
    if(!(*allLogVec[count]).getAuthenticate())
    {
        cout << endl << "User Not Authenticate! Please Authenticate your details below: " << endl;
        
        bool correct = false;
        
        while(!correct)
        {
            bool optionC = false;
            
            while(!optionC)
            {
                cout << endl;
                
                if((*allLogVec[count]).getType() == "member")
                {
                    cout << "Member Id     : " << (*allLogVec[count]).getMemberID() << endl;
                    cout << "Name          : " << (*allLogVec[count]).getMemberName() << endl;
                }
                else if ((*allLogVec[count]).getType() == "admin")
                {
                    cout << "Admin Id      : " << (*allLogVec[count]).getMemberID() << endl;
                    cout << "Name          : " << (*allLogVec[count]).getMemberName() << endl;
                }
            
                cout << "Is the above information correct(y/n) : ";
            
                string ch;
            
                cin >> ch;
            
                ch = tolower(ch[0]);
                
                if(ch.size() > 1)
                    cout << "Error: Detected an sentence! Re-enter option!" << endl << endl;
                else if (!((ch != "y") || (ch != "n")))
                    cout << "Error: Detected an Invalid option! Re-enter option!" << endl << endl;
                else
                {
                    if((ch == "y"))
                    {
                        optionC = true;
                        correct = true;
                        (*allLogVec[count]).setAuthenticate(true);
                    }
                    else if(ch == "n")
                    {
                        string userKeyStr;
                        char* userKey = new char;
                        userKey[0] = ' ';
     
                        while(userKey[0] != 'c')
                        {
                            cout << endl << "Choose From the Options Below To Change: " << endl;
                            cout << "a)Member Id " << endl;
                            cout << "b)Name" << endl;
                            cout << "c)Back to Prev Menu" << endl;

                            cout << endl << "Your Option : " ;
                            cin >> userKeyStr;
                            strcpy(userKey,userKeyStr.c_str());
            
                            if(userKeyStr.length() > 1)
                                cout << "Error: Detected a Sentence. Retry!" << endl << endl;
                            else if((userKey[0] = tolower(userKey[0])) && (!(userKey[0] >= 'a' && userKey[0] <= 'c')))
                            {
                                cout << "Error : Invalid Option!. Retry!" << endl << endl;
                            }
                            else
                            {
                                string newMemId,newMemName;
                                bool correct = false;
                                cout << endl;
                                
                                switch(userKey[0])
                                {
                                    case 'a' : 
                                                cout << "Old Member ID : " << (*allLogVec[count]).getMemberID() << endl;
                                                cout << "New Member ID (Enter number only) : C";
                                               
                                                while(!correct)
                                                {
                                                    cin >> newMemId;
                                               
                                                    bool allDigit = true;
                                                    
                                                    int i = 0;
                                                    
                                                    while(i < newMemId.size() && allDigit)
                                                    {
                                                        if(!(isdigit(newMemId[i])))
                                                            allDigit = false;
                                                        
                                                        ++i;
                                                    }
                                                    
                                                    if(!allDigit)
                                                        cout << "Error: Detect alphabets! Only Numbers! Re-enter Member ID : C";
                                                    else if(newMemId.length() > 5)
                                                        cout << "Error: Only 5 Digits are allowed! Re-enter Member ID : C";
                                                    else
                                                    {
                                                        bool unique = true;
                                                        vector<MemberAccount*>::iterator p = allLogVec.begin();
                                                        newMemId = "C" + newMemId;
                                                        
                                                        while(p != allLogVec.end() && unique)
                                                        {
                                                            string haveId = (*p)->getMemberID();
                                                            
                                                            if(haveId == newMemId)
                                                                unique = false;
                                                            
                                                            ++p;
                                                        }
                                                        
                                                        if(!unique)
                                                            cout << "Error: Member Id already exist! Re-enter Member ID : C";
                                                        else
                                                        {
                                                            correct = true;
                                                            (*allLogVec[count]).setMemberID(newMemId);
                                                        }
                                                    }
                                               }
                                               
                                        break;
                                        
                                    case 'b' : 
                                                cout << "Old Member Name : " << (*allLogVec[count]).getMemberName() << endl;
                                                cout << "New Member Name : ";
                                               
                                                cin.clear();
                                                cin.ignore(100,'\n');
                                                
                                                getline(cin,newMemName);
                                               
                                                (*allLogVec[count]).setMemberName(newMemName); 
                                        break;
                                        
                                    case 'c' : cout << "Returning to Prev Menu ... " << endl; 
                                        break;
                                }    
                            }
                        }
                    }
                }
            }
         }   
    }
   
   return (*allLogVec[count]);
}

void mainDriver::logout()
{
    fstream outfile;
    
    outfile.open("authorization.txt", ios::out);
    
    vector<MemberAccount*>::iterator p;
    p= allLogVec.begin();
    
    string d_cusName,d_cusId,d_cusPwd;
    string c_cusName,c_cusId,c_cusPwd;
    
    while(p != (this->allLogVec).end())
    {
        d_cusId = (*p)->getMemberID();
        d_cusName = (*p)->getMemberName();
        d_cusPwd = (*p)->getMemberPassword();
        
        //c_cusId = encryptLoginDetails(d_cusId);
        //c_cusName = encryptLoginDetails(d_cusName);
        //c_cusPwd = encryptLoginDetails(d_cusPwd);
        
        outfile << d_cusId << "|" 
                << d_cusName << "|"
                << d_cusPwd  << "|" 
                //<< encryptLoginDetails("member") << " ";
                << (*p)->getRewardPoints() << "|"
                << "member" << "|";
        
        if((*p)->getAuthenticate())
        {
            //string ok = encryptLoginDetails("ok");
            outfile << "ok" << endl;
        }
        else
        {
            //string ok = encryptLoginDetails("notOk");
            outfile << "notOk" << endl;
        }
        
        ++p;               
    }
    
    outfile.close();
}

string mainDriver::encryptLoginDetails(string str)
{
    for(int i = 0; i < str.length();i++)
    {
        if(isupper(str[i]))
        {
            if(isalpha(str[i]))
            {
                for(int j = 0; j < 13;j++)
                {
                    if(str[i] == 'Z')
                        str[i] = 'A';
                    else
                        str[i]++;
                }
            }
        }
        else
        {
            if(isalpha(str[i]))
            {
                for(int j = 0; j < 13;j++)
                {
                    if(str[i] == 'z')
                        str[i] = 'a';
                    else
                        str[i]++;
                }
            }
        }
    }
    
    return str;
}

string mainDriver::decryptLoginDetails(string str)
{

    for(int i = 0; i < str.length();i++)
    {
        if(isupper(str[i]))
        {
            if(isalpha(str[i]))
            {
                for(int j = 0; j < 13;j++)
                {
                    if(str[i] == 'Z')
                        str[i] = 'A';
                    else
                        str[i]++;
                }
            }
        }
        else
        {
            if(isalpha(str[i]))
            {
                for(int j = 0; j < 13;j++)
                {
                    if(str[i] == 'z')
                        str[i] = 'a';
                    else
                        str[i]++;
                }
            }
        }
    }
    
    return str;
}
